<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
class UserController extends Controller
{       
    
      //   后台用户列表页
    public function getIndex(Request $request){
      if(empty(session('id'))){
        return redirect('/admin')->with('error','请您登陆');
      }
        // 分页
        $count = $request -> input('count',10);
       // 关键字
        $search = $request -> input('search','');
        // dd(DB::table('users') -> get());
        // 对查询语句构造器进行分页  paginate  查询数据并且分页
        $data = DB::table('users') ->where('username','like','%'.$search.'%')-> paginate($count);
        // dd($data);
        return view('admin/user/index ',['data'=>$data,'count'=>$count,'search'=>$search,'request'=>$request->all()]);
    }
    // 用户添加页
    public function getAdd(){
        return view('admin/user/add');
    }
    // 用户添加
    public function postInsert(Request $request){
     
        // 进行自动表单验证  表单值存在闪存中
           $this->validate($request, [
            'username' => 'required',
            'password' => 'required',
            'repassword' => 'required|same:password',
            'email' => 'email',
            'tel' => 'required',
         ],[
            'username.required' => '用户名不存在',
            'password.required' => '密码不存在',
            'repassword.required' => '确认密码不存在',
            'repassword.same' => '确认密码不一致',
            'email.email' => '邮箱格式不正确',
            'tel.required'=>'手机号不存在',
        ]);
       
            // 数据组装
        
         $arr = $request -> only(['username']);
         
          // 获取指定参数进行哈希密码加密 
        $arr['password'] = Hash::make($request->input('password'));
         $arr['email'] = $request -> input('email');

        // token 随机数
        $arr['token'] = str_random(50);
        
         $arr['createtime'] = date('Y-m-d H:i:s',time()); 
         $arr['tel'] = $request -> input('tel');
        $arr['power'] = $request -> input('power');
          $arr['disable'] = $request -> input('disable');
        // 创建时间
       
        // 头像
       if($request -> hasFile('upic')){
        
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('upic') -> getClientOriginalExtension();
            // 拼接文件名
            $name = $temp_name.'.'.$hz;
            $request -> file('upic') -> move('./upload',$name);
            $arr['upic'] = $name;  
        }
        
       //进行添加
       $res = DB::table('users') -> insert($arr);
      // dd($res);
       if($res){
            return redirect('/admin/user/index') -> with('success','添加成功');
       }else{
            return back() -> with('error','添加失败');
       } 
     
    }
    // 用户删除
    public function getDelete($id){
       $data =  DB::table('users') -> where('id',$id)-> delete();
        if($data = 1){

            return redirect('/admin/user/index') -> with('success','删除成功');
       }else{
            return back() -> with('error','删除失败');
       } 
    }
    // 用户修改页
    public function getEdit($id){
    //查询单条数据
    $data = DB::table('users') ->where('id',$id) -> first(); 
  return view('admin/user/edit',['data'=>$data]);
    }
    // 用户修改
    public function postUpdate(Request $request){
       // 进行自动表单验证  表单值存在闪存中
           $this->validate($request, [
            'username' => 'required',
            'email' => 'email',
      

        ],[
            'username.required' => '用户名不存在',
            'email.email' => '邮箱格式不正确',
           
        ]);
        $arr = $request -> only(['username','email','tel','power','disable']);
      // dd($arr);
        // 获取id时
        $id = $request -> input('id');
          if($request -> hasFile('upic')){
            // dd(1);
            // 随机文件名
            $temp_name = md5(time().rand(100000,999999));
            // 获取文件后缀名
            $hz = $request -> file('upic') -> getClientOriginalExtension();
            // 拼接文件名
            $name = $temp_name.'.'.$hz;
            $request -> file('upic') -> move('./upload',$name);
             $arr['upic'] = $name;  
        }

        $data = DB::table('users') ->where('id',$id)-> update($arr);
          if($data = 1){
            
            return redirect('/admin/user/index') -> with('success','修改成功');
       }else{
            return back() -> with('error','修改失败');
       } 
    }
}
